package com.cne.gmail.Gmail.Util;


import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import lombok.extern.log4j.Log4j2;

import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.TimeUnit;
@Log4j2
public class CacheUtil {
    private static  Cache<Object, Object> cache = CacheBuilder.newBuilder()
                                                                .maximumSize(500)
                                                                .expireAfterAccess(7, TimeUnit.DAYS)
                                                                .build();
    private static  Cache<Object, Object> cacheToken = CacheBuilder.newBuilder()
            .maximumSize(500)
            .expireAfterAccess(3, TimeUnit.HOURS)
            .build();
     //缓存添加数据
     public static Object CacheEmail(String emailAddress,String historyId){
         cache.put(emailAddress,historyId);
         Object o = cache.asMap().get(emailAddress);
         return o;
     }
    //缓存删除数据
    public static Object removeEmailAdress(String emailAdress){
        Object remove = cache.asMap().remove(emailAdress);
        return remove;
    }
    //得到当前所有的缓存对象
    public static  ConcurrentMap<Object, Object> getCache(){
        ConcurrentMap<Object, Object> objectObjectConcurrentMap = cache.asMap();
        return objectObjectConcurrentMap;
    }
    //缓存添加数据
    public static Object CacheToken(String emailAddress,String token){
        cache.put(emailAddress+"_token",token);
        Object o = cache.asMap().get(token);
        return o;
    }
    //缓存删除数据
    public static Object removeToken(String token){
        Object remove = cache.asMap().remove(token);
        return remove;
    }
    //缓存登录的token值，设置过期时间为3个小时
    public static void CacheLoginToken(String CacheToken){
        cacheToken.put(CacheToken+"_login",CacheToken);
    }
    //获取缓存登录的token值，设置过期时间为3个小时
    public static boolean GetCacheLoginToken(String CacheToken){
        Object o = cacheToken.asMap().get(CacheToken+"_login");
        log.info(o);
        if (o==null||o.equals("")){
            return false;
        }else {
            return true;
        }
    }
}
